var contextPath = $("#contextPath").val();
var view=$("#picture")
$(function(){
	goodsDetail();
})

function goodsDetail(){
	var id=$("#goodsId").val();
	$.ajax({
		url:'goods/detail',
		data:{'id':id},
		type:"post",
		async:true,
		dataType:"json",
		success : function(data, textStatus) {
			if(data.obj != undefined){
				if(data.obj.picture != undefined){
				var pictures = data.obj.picture.split(',');
					$.each(pictures,function(i,v){
						view.append('<div class="swiper-slide"><a href="#"><img src="' + pictures[i] + '" alt=""></a></div>');
					});
				} else if(data.obj.thumb != undefined){
					view.append('<div class="swiper-slide"><a href="#"><img src="' + data.obj.thumb + '" alt=""></a></div>');
				}
				//商品图预览
				preview = new Swiper('.swiper-container', {
				  loop: true,
				  autoplay: 3000,
				  autoHeight: true,
				  pagination: '.swiper-pagination',
				  paginationClickable: true
				});
				$("#title").html(data.obj.title);
				$("#describe").html(data.obj.describe);
				prices = data.price;
				$.each(prices,function(i,v){
					if(v.minNum ==1){
						$("#price").html(v.price);
					}
				})
				$("#packing").html('<div class="cell__hd g9">包装规格</div><div class="cell__bd">'+data.obj.packing+'</div>');
				$("#fruitPath").html('<div class="cell__hd g9">果&emsp;&emsp;径</div><div class="cell__bd">'+data.obj.fruitPath+'</div>');
				$("#content").html(data.obj.content);
				document.getElementById("vip_tips").style.display = "none";
				
				goodsSpecList = data.goodsSpecList;
				ratioConfigure = data.ratioConfigure;
				var gl = data.rSpecType;
				if(gl!=null && gl.length>0){
					var glHtml="";
					$.each(gl,function(gli,glv){
						glHtml +='<div class="goods_options_item" id="specification">'+
								'<span class="goods_options_name">'+glv.label_name+'</span>'+
								'<div class="goods_options_box"> <div class="options_box_list">';
						$.each(glv.rSpecDetail,function(glai,glav){
							if(glai == 0){
								glHtml +='<span id='+glav.id+' class="options_box_item options_box_item_active">'+glav.attr_name+'</span>';
							}else{
								glHtml +='<span id='+glav.id+' class="options_box_item">'+glav.attr_name+'</span>';	
							}
						})
						glHtml +='</div> </div></div>';
					});
					glHtml +='<div class="goods_options_item">'+
					      
					   '</div>';
					goodsSpec.append(glHtml);
					 new Compute();
					isSales = data.obj.isSales;
					var bidPrice = goodsSpecList[getSpec()].bid_price;
					var price = goodsSpecList[getSpec()].price;
					var marketPrice = goodsSpecList[getSpec()].market_price;
					var salesPrice = goodsSpecList[getSpec()].salesPrice;
					if(isPointsBuy == "01"){
						if(isSales == "00"){
							var changePoints = getReqPoints(bidPrice,marketPrice,price);
							var discount = toDecimal(bidPrice/marketPrice);
							var minRatio = ratioConfigure.min_ratio;
							var changePrice = 0;
							if(discount > minRatio){
								changePrice = (price-changePoints).toFixed(2);
							}
							
							$(".npoints").html(changePoints);
							$(".money").html(changePrice);
							$(".sellPrice").html(price);
						}else{
							var changePoints = getReqPoints(bidPrice,marketPrice,salesPrice);
							var discount = toDecimal(bidPrice/marketPrice);
							var minRatio = ratioConfigure.min_ratio;
							var changePrice = 0;
							if(discount > minRatio){
								changePrice = (price-changePoints).toFixed(2);
							}
							$(".npoints").html(changePoints);
							$(".money").html(changePrice);
							$(".sellPrice").html(salesPrice);
						}
					}else{
						if(isSales == "00"){
							$(".normal").html("&yen; " + price);
						}else{
							$(".normal").html("&yen; " + salesPrice);
						}
					}
					
				}else{
					var  buyNumHtml ='<div class="goods_options_item">'+
				      '<span class="goods_options_name">数量</span>'+
				      '<div class="goods_options_box">'+
				        '<div class="options_box_list">'+
				         '<span class="quantity dib vm">'+
				               '<a class="quantity_subtract dib vm" href="javascript:;">-</a>'+
				              '<input class="quantity_input dib vm" type="text" value="1" min="0" name="">'+
				              '<a class="quantity_add dib vm" href="javascript:;">+</a>'+
				            '</span>'+
				        '</div>'+
				      '</div>'+
				   '</div>';
				goodsSpec.append('');
				new Compute();
				}
				if(data.obj.content == undefined) {
					$("#goodsContext").html(" 无");
				}else {
					$("#goodsContext").html(data.obj.content);
				}
				goodsInfo.show();
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 2
			});
		}
	});	
}